//
//  HMNetworTools.swift
//  04-Swift网络封装
//
//  Created by Apple on 2017/5/30.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestType {
    case get
    case post
}

class HMNetworTools: AFHTTPSessionManager {

    static let sharedTools: HMNetworTools = {
        let tools = HMNetworTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        return tools
    }()
    
    //  通用的网络请求
    fileprivate func requestData(urlStr: String, params: Any?, type: RequestType, callBack: @escaping (Any?, Error?)->()) {
        if type == .get {
            get(urlStr, parameters: params, progress: nil, success: { (_, response) in
                callBack(response, nil)
            }) { (_, error) in
                callBack(nil, error)
            }
            
        } else {
            post(urlStr, parameters: params, progress: nil, success: { (_, response) in
                callBack(response, nil)
            }, failure: { (_, error) in
                callBack(nil, error)
            })
        }
    }
    
}

//  MARK:   --首页相关接口
extension HMNetworTools {
    
    /// 获取首页微博数据
    ///
    /// - Parameters:
    ///   - accessToken: 令牌
    ///   - callBack: 闭包
    func requestStatuses(accessToken: String, callBack: @escaping (Any?, Error?) -> ()) {
        //  接口地址
        let urlStr = "https://api.weibo.com/2/statuses/home_timeline.json"
        //  请求参数
        let params = [
            "access_token": accessToken
        ]
        print(urlStr + "?access_token=\(accessToken)")
        //  发起网络请求
        requestData(urlStr: urlStr, params: params, type: .get, callBack: callBack)
    }
}

//  MARK:   --OAuth登录相关接口
extension HMNetworTools {
    
    /// 获取用户信息
    ///
    /// - Parameters:
    ///   - accessToken: 令牌
    ///   - uid: 用户id
    ///   - callBack: 闭包
    func requestUserInfo(accessToken: String, uid: Int64, callBack: @escaping (Any?, Error?) -> ()) {
        //  请求地址
        let urlStr = "https://api.weibo.com/2/users/show.json"
        //  请求参数
        let params: [String: Any] = [
            "access_token": accessToken,
            "uid": uid
        ]
        //  发起网络请求
        requestData(urlStr: urlStr, params: params, type: .get, callBack: callBack)
        
    }
    
    /// 根据授权码获取accesstoken
    ///
    /// - Parameters:
    ///   - code: 授权码
    ///   - callBack: 闭包
    func requestAccessToken(code: String, callBack: @escaping (Any?, Error?) -> ()) {
        //  请求地址
        let urlStr = "https://api.weibo.com/oauth2/access_token"
        //  请求参数
        let params = [
            "client_id": AppKey,
            "client_secret": Appsecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": RedirectUrl
        ]
        //  发送网络请求
        requestData(urlStr: urlStr, params: params, type: .post, callBack: callBack)
    }
}
